<template>
  <!--  父子传值
       ref方式
       基本父子传值props与emit
            v-model
            .sync
               this.$emit('update:visible', true)
     -->
  <el-dialog
    :title="{ add: '新增权限点', edit: '编辑权限点' }[mode]"
    :visible="value"
    @close="chiEvale"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="开启" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button type="primary" @click="abmit">确定</el-button>
        <el-button @click="chiEvale">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissIonPost, sysPermissIonPut } from '@/api/permission'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      text: true,
      mode: '',
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: '',
        pid: ''
      },
      // 必填
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    chiEvale () {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: '',
        pid: ''
      }
    },
    abmit () {
      this.$refs.form.validate(async result => {
        if (result) {
          if (this.mode === 'add') {
            await sysPermissIonPost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await sysPermissIonPut(this.form)
            this.$message.success('编辑成功')
          }
          this.chiEvale()
          this.$emit('getData')
          console.log(result)
        }
      })
    }
  }
}
</script>

<style></style>
